//
//  YFThreeDTemplate.m
//  SquashSpark
//
//  Created by Coollang on 2017/11/30.
//  Copyright © 2017年 Coollang-YF. All rights reserved.
//

#import "YFThreeDTemplate.h"

@implementation YFThreeDTemplate

+(NSString *)primaryKey{
    return @"identification";
}
+ (NSArray<NSString *> *)ignoredProperties {
    return @[@"isDeletSelect"];
}

- (instancetype)initWithThreeDitem:(YFThreeDItem *)threeDItem name:(NSString *)name templateImage:(NSString *)image mainType:(TemplateMainType)mainType {
    if (self = [super init]) {
        self.threeDItem = threeDItem;
        self.name = name;
        self.templateImage = image;
        self.creatTimestamp = threeDItem.unixTimeStamp;
        self.creatDate = threeDItem.dateString;
        self.mainType = mainType;
        self.userId = [User getLastUser].userID;
        self.identification = [NSString stringWithFormat:@"%@_%lld",self.userId,self.creatTimestamp];
    }
    return self;
}



- (void)addMatchSwing:(YFThreeDItem *)matchSwingItem {
    if ([YFThreeDItem objectForPrimaryKey:matchSwingItem.identification]) {
        return;
        YFLog(@"重复- -- -- - -  - -- - 舍弃");
    }
    [[RLMRealm defaultRealm] transactionWithBlock:^{
        [self.matchSwingArray addObject:matchSwingItem];
    }];
}

/**
 * 获取当前用户下的所有模板
 */
+ (RLMResults *)getCurrentUserAllTemplateModels {
    return [YFThreeDTemplate objectsWhere:@"userId == %@",[User getLastUser].userID];
}

/**
 * 获取当前用户下的所有系统模板
 */
+ (RLMResults *)getCurrentUserAllSystemTemplateModels {
    RLMResults *allTemplates = [YFThreeDTemplate objectsWhere:@"userId == %@",[User getLastUser].userID];
    return [allTemplates objectsWhere:@"mainType == %d",TemplateMainTypeSystem];
}
/**
 * 获取当前用户下的所有自定义模板
 */
+ (RLMResults *)getCurrentUserAllCustomTemplateModels {
    RLMResults *allTemplates = [YFThreeDTemplate objectsWhere:@"userId == %@",[User getLastUser].userID];
    return [allTemplates objectsWhere:@"mainType == %d",TemplateMainTypeMy];
}

/**
 * 将模板模型存入用户数据库
 */
+ (void)saveTemplateToDocument:(YFThreeDTemplate *)temItem {
    if (temItem.identification == nil) {
        return;
    }
    YFThreeDTemplate *templateItem = [YFThreeDTemplate objectForPrimaryKey:temItem.identification];
    if (templateItem == nil) {
        RLMRealm *realm = [RLMRealm defaultRealm];
        [realm beginWriteTransaction];
        [realm addOrUpdateObject:temItem];
        [realm commitWriteTransaction];
    }
}

+ (NSString *)templateWithHandType:(HandDirectionType)handType selectActionType:(kSportActionType)actionType {
    NSString *handStr = handType == HandDirectionTypeForeward ? @"Forehand":@"Backhand";
    return [NSString stringWithFormat:@"%@-%@",handStr,[SquashEnum stringWithSportActionType:actionType]];
}

@end

